<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>网页计算器</title>
    <style>
        .calculator {
            width: 320px;
            margin: 50px auto;
            padding: 20px;
            border: 1px solid #ccc;
            border-radius: 10px;
            background-color: #f8f8f8;
        }

        #display {
            width: 100%;
            height: 60px;
            margin-bottom: 10px;
            padding: 10px;
            font-size: 24px;
            text-align: right;
            border: 1px solid #ddd;
            border-radius: 5px;
        }

        .buttons {
            display: grid;
            grid-template-columns: repeat(4, 1fr);
            gap: 5px;
        }

        button {
            padding: 15px;
            font-size: 18px;
            border: 1px solid #ddd;
            border-radius: 5px;
            background-color: #fff;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #eee;
        }

        .operator {
            background-color: #f0f0f0;
        }

        .equals {
            background-color: #4CAF50;
            color: white;
        }

        .clear {
            background-color: #f44336;
            color: white;
        }
    </style>
</head>
<body>
    <div class="calculator">
        <input type="text" id="display" readonly>
        <div class="buttons">
            <button class="clear" onclick="clearDisplay()">C</button>
            <button onclick="appendToDisplay('(')">(</button>
            <button onclick="appendToDisplay(')')">)</button>
            <button class="operator" onclick="appendToDisplay('/')">/</button>
            
            <button onclick="appendToDisplay('7')">7</button>
            <button onclick="appendToDisplay('8')">8</button>
            <button onclick="appendToDisplay('9')">9</button>
            <button class="operator" onclick="appendToDisplay('*')">×</button>
            
            <button onclick="appendToDisplay('4')">4</button>
            <button onclick="appendToDisplay('5')">5</button>
            <button onclick="appendToDisplay('6')">6</button>
            <button class="operator" onclick="appendToDisplay('-')">-</button>
            
            <button onclick="appendToDisplay('1')">1</button>
            <button onclick="appendToDisplay('2')">2</button>
            <button onclick="appendToDisplay('3')">3</button>
            <button class="operator" onclick="appendToDisplay('+')">+</button>
            
            <button onclick="appendToDisplay('0')">0</button>
            <button onclick="appendToDisplay('.')">.</button>
            <button class="equals" onclick="calculate()" style="grid-column: span 2">=</button>
        </div>
    </div>

    <script>
        function appendToDisplay(value) {
            document.getElementById('display').value += value;
        }

        function clearDisplay() {
            document.getElementById('display').value = '';
        }

        function calculate() {
            const display = document.getElementById('display');
            try {
                let expression = display.value
                    .replace(/×/g, '*')
                    .replace(/÷/g, '/');
                
                if(/[^0-9+\-*/().]/.test(expression)) {
                    throw new Error("包含无效字符");
                }
                
                const result = eval(expression);
                if(!isFinite(result)) {
                    throw new Error("计算结果无效");
                }
                display.value = result;
            } catch (error) {
                display.value = '错误';
                setTimeout(clearDisplay, 1000);
            }
        }
    </script>
</body>
</html>